package com.ruoyi.web.controller.snmp;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.SnmpCpu;
import com.ruoyi.system.service.ISnmpCpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 *
 * @author ruoyi
 * @date 2025-02-05
 */
@RestController
@RequestMapping("/snmp/cpu")
public class SnmpCpuController extends BaseController
{
    @Autowired
    private ISnmpCpuService snmpCpuService;

    /**
     * 查询列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:cpu:list')")
    @GetMapping("/list")
    public TableDataInfo list(SnmpCpu snmpCpu)
    {
        startPage();
        List<SnmpCpu> list = snmpCpuService.selectSnmpCpuList(snmpCpu);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @PreAuthorize("@ss.hasPermi('snmp:cpu:export')")
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SnmpCpu snmpCpu)
    {
        List<SnmpCpu> list = snmpCpuService.selectSnmpCpuList(snmpCpu);
        ExcelUtil<SnmpCpu> util = new ExcelUtil<SnmpCpu>(SnmpCpu.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('snmp:cpu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(snmpCpuService.selectSnmpCpuById(id));
    }


    @GetMapping(value = "/byip/{ip}")
    public AjaxResult getInfobyIP(@PathVariable("ip") String ip) throws Exception {
        List<SnmpCpu>  arry=snmpCpuService.GetCpuInfoOne(ip);
        if(arry==null||arry.toArray().length<1||arry.get(0).getId().equals("")) {
            arry = snmpCpuService.GetInfo(ip);
        }
        SnmpCpu entity=new SnmpCpu();
        if(arry.toArray().length>0) {
            entity = arry.get(0);
        }
        return success(entity);
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('snmp:cpu:add')")
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SnmpCpu snmpCpu)
    {
        return toAjax(snmpCpuService.insertSnmpCpu(snmpCpu));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('snmp:cpu:edit')")
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SnmpCpu snmpCpu)
    {
        return toAjax(snmpCpuService.updateSnmpCpu(snmpCpu));
    }

    /**
     * 删除【请填写功能名称】
     */
    @PreAuthorize("@ss.hasPermi('snmp:cpu:remove')")
    @Log(title = "", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(snmpCpuService.deleteSnmpCpuByIds(ids));
    }
}
